package org.TCPcase4;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyRunnable implements Runnable{
    private Socket socket;

    public MyRunnable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());
            String name= UUID.randomUUID().toString().replace("-","");
            BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("C:\\Users\\夏明毅\\Desktop\\java课程代码\\mySocketnet\\serverDir\\"+name+".text"));
            int len=0;
            byte [] data=new byte[1024];
            while ((len=bis.read(data))!=-1){
                bos.write(data,0,len);
                bos.flush();
            }
            String str="数据上传成功";
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(str);
            bw.newLine();
            bw.flush();
            socket.shutdownOutput();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
